/**
 * 
 */
package badugi;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author aleks
 *
 */
public class Hand {
	private Card[] hand= new Card[4];
	private int indexOfCardToChange = 4;

	/**
	 * @return the hand
	 */
	public Card[] getHand() {
		return hand;
	}
	
	public void sortHand() {
		Arrays.sort(hand);
	}
	
	public int getColorOfTheCard(int index) {
		return hand[index].getColor();
	}
	
	public int getValueOfTheCard(int index) {
		return hand[index].getValue();
	}
	
	public Card getOneCard(int index) {
		return hand[index];
	}

	Hand(Card one, Card two, Card three, Card four) {
		this.hand[0] = one;
		this.hand[1] = two;
		this.hand[2] = three;
		this.hand[3] = four;
	}
	
	public Card giveMeTheCardToThrowOut(int whichCardThrowOut) throws HandException {
		if (whichCardThrowOut < 0 || whichCardThrowOut > 3) throw new HandException(0);
		indexOfCardToChange = whichCardThrowOut;
		//Card tempCard = hand[whichCardThrowOut];
		//hand[whichCardThrowOut] = newCard;
		return hand[whichCardThrowOut];
	}
	
	public void addNewCardFromTheChange(int whichCardWasThrowOut, Card newCard) throws HandException {
		if (whichCardWasThrowOut < 0 || whichCardWasThrowOut > 3) throw new HandException(0);
		if (whichCardWasThrowOut != indexOfCardToChange) throw new HandException(1);
		hand[whichCardWasThrowOut] = newCard;
		indexOfCardToChange = 4;
	}
	
	public void mixFirstAndLastCard() {
		ArrayList<Card> temp = new ArrayList<Card>();
		for (int i = 0; i < hand.length; i++) {
			temp.add(hand[i]);
		}
		Card tempCard = temp.remove(0);
		temp.add(tempCard);
		hand = temp.toArray(hand);
	}
	
	public String toString() {
		String string = "";
		for (int i = 0; i < 4; i++) {
			string += hand[i].toString();
			if (i != 3) {
				string += "/";
			}
		}
		return string;
	}
	
	
}
